package com.onenine.app.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.onenine.app.R;
import com.onenine.app.activity.UserDetailActivity;
import com.onenine.app.adapter.FriendListAdapter;
import com.onenine.app.event.RefreshEvent;
import com.onenine.app.model.MemberItem;
import com.onenine.app.request.PageRequest;
import com.onenine.app.response.FriendAttentionResponse;
import com.onenine.app.utils.CDataProcessor;
import com.onenine.app.utils.DataType;
import com.onenine.app.utils.UrlConfig;
import com.onenine.base.base.BasePageListFragment;
import com.onenine.base.okhttp.OkCallback;
import com.onenine.base.okhttp.OkRequest;
import com.onenine.base.utils.AppConfig;
import com.onenine.base.utils.Toasty;
import com.onenine.base.view.PullRefreshRecyclerView;
import com.onenine.chat.rongcloud.RcUser;
import com.onenine.dialog.CommonTextDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class FriendAttentionFragment extends BasePageListFragment implements BaseQuickAdapter.OnItemChildClickListener {

    private int mType;//0好友1关注

    public static Fragment newInstance(int type) {
        FriendAttentionFragment fragment = new FriendAttentionFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Pair<PullRefreshRecyclerView, RecyclerView.Adapter> initPullRefreshView(View view) {
        mType = getArguments().getInt("type");
        return Pair.create(view.findViewById(R.id.pull_to_refresh_layout)
                , new FriendListAdapter(mType));
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initListener() {
        super.initListener();
        ((BaseQuickAdapter) mAdapter).setOnItemChildClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadPageData(1);
    }

    @Override
    protected void loadPageData(int pageNum) {
        PageRequest request = new PageRequest();
        request.setUserId(RcUser.getInstance().getUserId());
        request.setPageNum(pageNum);
        request.setPageSize(AppConfig.PAGE_SIZE);
        OkRequest.getInstance().withUrl(mType == 0 ? UrlConfig.GET_FRIEND_LIST : UrlConfig.GET_ATTENTION_LIST).withRequest(request)
                .doRequest(FriendAttentionFragment.class.getSimpleName(), new OkCallback<FriendAttentionResponse>() {
                    @Override
                    public void onResponse(FriendAttentionResponse response, String srcData) {
                        dismissRefreshStatus();
                        if (response.isSuccess()) {
                            notifyDataSuccess(response.getDataList());
                        } else {
                            notifyDataFailure();
                        }
                    }
                });
    }

    @Override
    protected int getLayout() {
        return R.layout.layout_pull_refresh_view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(RefreshEvent event) {
        if (event.getType() == DataType.REFRESH_DELETE_FRIEND) {
            mPullRefreshView.removeData(event.extraInt);
            if (getData().isEmpty()) {
                mPullRefreshView.setPullEmptyView();
            }
            //重新同步好友UserId列表
            CDataProcessor.syncFriendIds();
            Toasty.showNormal(R.string.delete_success);
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (view.getId() == R.id.rl_view) {
            UserDetailActivity.launchActivity(getActivity()
                    , ((MemberItem) adapter.getItem(position)).getUserId());
        } else if (view.getId() == R.id.btn_delete) {
            showDelDialog(((MemberItem) adapter.getItem(position)), position);
        }
    }

    /**
     * 删除前确认
     *
     * @param friendItem
     * @param position
     */
    private void showDelDialog(final MemberItem friendItem, final int position) {
        new CommonTextDialog.Builder(getContext())
                .setText(R.string.delete_friend_hint)
                .setOnOkClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CDataProcessor.dealFriendRequest(DataType.FRIEND_DEAL_DELETE, friendItem.getUserId(), position);
                    }
                }).builder().show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        OkRequest.getInstance()
                .cancelTag(FriendAttentionFragment.class.getSimpleName());
    }

}
